package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private MovementService movementService;

    @DubboReference
    private CommentService commentService;

    @DubboReference
    private VideoService videoService;

    // 用户分页查询
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询分页
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);

        // 2.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 用户详情
    public ResponseEntity findUserInfoByUserId(Long userId) {
        // 1.调用rpc查询
        UserInfo userInfo = userInfoService.findById(userId);
        // 2.返回用户信息
        return ResponseEntity.ok(userInfo);
    }

    // 动态分页查询
    public ResponseEntity findMovementVoByPage(Long userId, Integer state, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = movementService.findMovementByPage(userId, state, pageNum, pageSize);
        // 2.封装movmenetVo
        List<MovementVo> voList = new ArrayList<>();
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                // 创建vo
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfoService.findById(movement.getUserId())); // 用户信息
                vo.setMovement(movement);// 动态详情
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated()))); // 发布时间 年月日 时分秒

                // 加入vo集合
                voList.add(vo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);

        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 动态详情查询
    public ResponseEntity findMovementVoById(String publishId) {
        // 1.调用rpc根据主键查询动态详情
        Movement movement = movementService.findById(new ObjectId(publishId));
        // 2.封装movmenetVo
        MovementVo movementVo = new MovementVo();
        UserInfo userInfo = userInfoService.findById(movement.getUserId());
        // 注意顺序：先userInfo  再 movement
        movementVo.setUserInfo(userInfo);
        movementVo.setMovement(movement);
        movementVo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated()))); // 发布时间
        // 3.返回vo
        return ResponseEntity.ok(movementVo);
    }

    // 动态评论分页查询
    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = commentService.findCommentByPage(publishId, pageNum, pageSize, 2);
        // 2.封装commentVo
        // 2-1 声明vo集合
        List<CommentVo> voList = new ArrayList<>();
        // 2-2 获取comment集合
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 2-3 遍历comment集合
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 查询评论人的userInfo
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                //  创建vo
                CommentVo commentVo = new CommentVo();
                commentVo.setId(comment.getId().toHexString()); // 评论id
                commentVo.setAvatar(userInfo.getAvatar()); // 评论人头像
                commentVo.setNickname(userInfo.getNickname()); // 评论人昵称
                commentVo.setContent(comment.getContent()); // 评论内容
                commentVo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 评论时间

                // 添加vo到集合中
                voList.add(commentVo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);
        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 视频分页查询
    public ResponseEntity findVideoVoByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = videoService.findVideoByUserId(userId, pageNum, pageSize);
        // 2.封装videoVo
        List<VideoVo> voList = new ArrayList<>();
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                //  创建vo
                VideoVo vo = new VideoVo();
                vo.setUserInfo(userInfoService.findById(video.getUserId())); // 用户信息
                vo.setVideo(video);

                // 添加vo集合中
                voList.add(vo);
            }
        }
        // 3.将vo设置到分页对象
        pageBeanVo.setItems(voList);
        // 4.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
}
